package com.XinYun.Library.modules.Login.Servlet;

import com.XinYun.Library.modules.Login.Service.Impl.UserServiceImpl;
import com.XinYun.Library.modules.Login.Service.UserService;
import com.XinYun.Library.modules.commons.ResponseWrapper.ResponseWrapper;
import com.XinYun.Library.utils.SqlUtils.DruidUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import static org.apache.poi.ss.formula.ptg.NamePtg.sid;

@WebServlet(name = "FindPwdServlet3", value = "/FindPwdServlet3")
public class FindPwdServlet3 extends HttpServlet {
    UserServiceImpl userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String readerLine = reader.readLine();
        JSONObject jsonObject = JSON.parseObject(readerLine);
        String pwd1 = jsonObject.getString("pwd1");
        String pwd2 = jsonObject.getString("pwd2");

        //String sid3 = "2022007056";
        HttpSession session = request.getSession();
        session.setAttribute("sid", sid);
        String sid2 = (String) session.getAttribute("sid");

        if (pwd1.equals(pwd2)) {
            try {
                if(userService.updatePwd(pwd1, sid2)){
                    ResponseWrapper responseWrapper = null;
                    responseWrapper = ResponseWrapper.ResetSuccess();
                    String s = JSON.toJSONString(responseWrapper);
                    PrintWriter writer = response.getWriter();
                    writer.println(s);
                    System.out.println(s);
                    //request.getRequestDispatcher("Login.jsp").forward(request,response);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else {
                ResponseWrapper responseWrapper = null;
                responseWrapper = ResponseWrapper.CopyFail();
                String s = JSON.toJSONString(responseWrapper);
                PrintWriter writer = response.getWriter();
                writer.println(s);
                System.out.println(s);
                //request.getRequestDispatcher("repassword3.jsp").forward(request,response);
        }
    }
}
